$(document).ready(function(){
    //Global vars
    var form = $("#customForm");
    var firstname = $("#firstname");
    var firstnameinfo = $("#firstnameinfo");
    var lastname = $("#lastname");
    var lastnameinfo = $("#lastnameinfo");
    var bornday = $("#bornday");
    var bornmonth = $("#bornmonth");
    var bornyear = $("#bornyear");
    var birthdateinfo = $("#birthdateinfo");
    var sex = $("#sex");
    var sexinfo = $("#sexindo");
    var maritalstatus = $("#maritalstatus");
    var maritalstatusinfo = $("#maritalstatusinfo");
    var medicallyfit = $("#medicallyfit");
    var medicallyfitinfo = $("#medicallyfitinfo");
    var termaddr1 = $("#termAddr1");
    var termaddr1info = $("#termAddr1info");
    var termaddr2 = $("#termAddr2");
    var termaddr2info = $("#termAddr2info");
    var termpostalcode = $("#termPostalCode");
    var termpostalcodeinfo = $("#termPostalCodeinfo");
    var mobilephone = $("#mobilephone");
    var mobilephoneinfo = $("#mobilephoneinfo");
    var homeaddr1 = $("#homeAddr1");
    var homeaddr1info = $("#homeAddr1info");
    var homeaddr2 = $("#homeAddr2");
    var homeaddr2info = $("#homeAddr2info");
    var homepostalcode = $("#homePostalCode");
    var homepostalcodeinfo = $("#homePostalCodeinfo");
    var homephone = $("#homephone");
    var homephoneinfo = $("#homephoneinfo");
    var nationality = $("#nationality");
    var nationalityinfo = $("#nationalityinfo");

    //On blur
    firstname.blur(validateFirstname);
    lastname.blur(validateLastname);
    bornday.blur(validateBornday);
    bornmonth.blur(validateBornmonth);
    bornyear.blur(validateBornyear);
    sex.blur(validateSex);
    maritalstatus.blur(validateMaritalStatus);
    medicallyfit.blur(validateMedicallyfit);
    termaddr1.blur(validateTermAddr1);
    termpostalcode.blur(validateTermPostalCode);
    mobilephone.blur(validateMobilephone);
    homeaddr1.blur(validateHomeAddr1);
    homepostalcode.blur(validateHomePostalCode);
    homephone.blur(validateHomephone);
    nationality.blur(validateNationality);
    //On key press
    firstname.keyup(validateFirstname);
    lastname.keyup(validateLastname);
    bornday.keyup(validateBornday);
    bornmonth.keyup(validateBornmonth);
    bornyear.keyup(validateBornyear);
    sex.keyup(validateSex);
    maritalstatus.keyup(validateMaritalStatus);
    medicallyfit.keyup(validateMedicallyfit);
    termaddr1.keyup(validateTermAddr1);
    termpostalcode.keyup(validateTermPostalCode);
    homeaddr1.keyup(validateHomeAddr1);
    homepostalcode.keyup(validateHomePostalCode);
    homephone.keyup(validateHomephone);
    nationality.keyup(validateNationality);
    //On Submitting
    form.submit(function(){
        if(validateFirstname() & validateLastname() & validateBornday() & validateBornmonth() & validateBornyear() & validateSex() & validateMaritalStatus() & validateMedicallyfit & validateTermAddr1() & validateTermPostalCode() & validateHomeAddr1() & validateHomePostalCode() & validateHomephone() & validateNationality())
            return true;
        else
            return false;
    });
    
    function validateFirstname(){
        //If it's not valid
        if(firstname.val().length < 4){
            firstname.addClass("error");
            firstnameinfo.text("We want firstname with more than 3 letters!");
            firstnameinfo.addClass("error");
            return false;
        }
        //if it's valid
        else{
            firstname.removeClass("error");
            firstnameinfo.text("What's your firstname?");
            firstnameinfo.removeClass("error");
            return true;
        }
    }

    function validateLastname(){
        //If it's not valid
        if(lastname.val().length < 4){
            lastname.addClass("error");
            lastnameinfo.text("We want lastname with more than 3 letters!");
            lastnameinfo.addClass("error");
            return false;
        }
        //if it's valid
        else{
            lastname.removeClass("error");
            lastnameinfo.text("What's your lastname?");
            lastnameinfo.removeClass("error");
            return true;
        }
    }

    function validateBornday() {
        //it's NOT valid
        if(bornday.val().length != 2){
            bornday.addClass("error");
            birthdateinfo.text("You entered a not valid birthdate!");
            birthdateinfo.addClass("error");
            return false;
        }
        //it's valid
        else {
            bornday.removeClass("error");
            birthdayinfo.text("Birthday should be  a valid date!");
            birthdayinfo.removeClass("error");
            return true;
        }
    }

    function validateBornmonth() {
        //it's NOT valid
        if(bornmonth.val().length != 2){
            bornmonth.addClass("error");
            birthdateinfo.text("You entered a not valid birthdate!");
            birthdateinfo.addClass("error");
            return false;
        }
        //it's valid
        else {
            bornmonth.removeClass("error");
            birthdayinfo.text("Birthday should be  a valid date!");
            birthdayinfo.removeClass("error");
            return true;
        }
    }

    function validateBornyear() {
        //it's NOT valid
        if(bornyear.val().length != 2){
            bornyear.addClass("error");
            birthdateinfo.text("You entered a not valid birthdate!");
            birthdateinfo.addClass("error");
            return false;
        }
        //it's valid
        else {
            bornyear.removeClass("error");
            birthdayinfo.text("Birthday should be  a valid date!");
            birthdayinfo.removeClass("error");
            return true;
        }
    }

    function validateSex(){
        //it's NOT valid
        if(sex.val()==null){
            sex.addClass("error");
            sexinfo.text("You have to choose sex!");
            sexinfo.addClass("error");
            return false;
        }
        //it's valid
        else{
            sex.removeClass("error");
            sexinfo.text("Choose your sex!");
            sexinfo.removeClass("error");
            return true;
        }
    }

    function validateMaritalStatus(){
        //it's NOT valid
        if(maritalstatus.val()==null){
            maritalstatus.addClass("error");
            maritalstatusinfo.text("You have to choose marital status!");
            maritalstatusinfo.addClass("error");
            return false;
        }
        //it's valid
        else{
            maritalstatus.removeClass("error");
            maritalstatusinfo.text("Choose your marital status!");
            maritalstatusinfo.removeClass("error");
            return true;
        }
    }

    function validateMedicallyfit(){
        //it's NOT valid
        if(medicallyfit.val()==null){
            medicallyfit.addClass("error");
            medicallyfitinfo.text("You have to choose yes or no!");
            medicallyfitinfo.addClass("error");
            return false;
        }
        //it's valid
        else{
            medicallyfit.removeClass("error");
            medicallyfitinfo.text("Choose you medically status!");
            medicallyfitinfo.removeClass("error");
            return true;
        }
    }

    function validateTermAddr1() {
        //it's NOT valid
        if(termaddr1.val().length < 10){
            termaddr1.addClass("error");
            termaddr1info.text("Address line 1 have to be at least 10 letters!");
            termaddr1info.addClass("error");
            return false;
        }
        //it's valid
        else{
            termaddr1.removeClass("error");
            termaddr1info.text("Address line 1 is your address!");
            termaddr1info.removeClass("error");
            return true;
        }
    }

    function validateTermPostalCode(){
        //it's NOT valid
        if(termpostalcode.val().length != 4){
            termpostalcode.addClass("error");
            termpostalcodeinfo.text("You have to use an Norwegian postal code, that is 4 digits!");
            termpostalcodeinfo.addClass("error");
            return false;
        }
        //it's valid
        else{
            termpostalcode.removeClass("error");
            termpostalcodeinfo.text("Use an Norwegian postal code!");
            termpostalcodeinfo.removeClass("error");
            return true;
        }
    }

    function validateMobilephone(){
        //it's NOT valid
        if(mobilephone.val().length != 8){
            mobilephone.addClass("error");
            mobilephoneinfo.text("You have to use an Norwegian cell phone number, that is 8 digits!");
            mobilephoneinfo.addClass("error");
            return false;
        }
        //it's valid
        else{
            mobilephone.removeClass("error");
            mobilephoneinfo.text("Use an Norwegian cell phone number!");
            mobilephoneinfo.removeClass("error");
            return true;
        }
    }

    function validateHomeAddr1() {
        //it's NOT valid
        if(homeaddr1.val().length < 10){
            homeaddr1.addClass("error");
            homeaddr1info.text("Address line 1 have to be at least 10 letters!");
            homeaddr1info.addClass("error");
            return false;
        }
        //it's valid
        else{
            homeaddr1.removeClass("error");
            homeaddr1info.text("Address line 1 is your address!");
            homeaddr1info.removeClass("error");
            return true;
        }
    }

    function validateHomePostalCode(){
        //it's NOT valid
        if(homepostalcode.val().length != 4){
            homepostalcode.addClass("error");
            homepostalcodeinfo.text("You have to use an Norwegian postal code, that is 4 digits!");
            homepostalcodeinfo.addClass("error");
            return false;
        }
        //it's valid
        else{
            homepostalcode.removeClass("error");
            homepostalcodeinfo.text("Use an Norwegian postal code!");
            homepostalcodeinfo.removeClass("error");
            return true;
        }
    }

    function validateHomephone(){
        //it's NOT valid
        if(homephone.val().length != 8){
            mobilephone.addClass("error");
            homephoneinfo.text("You have to use an Norwegian cell phone number, that is 8 digits!");
            homephoneinfo.addClass("error");
            return false;
        }
        //it's valid
        else{
            homephone.removeClass("error");
            homephoneinfo.text("Use an Norwegian cell phone number!");
            homephoneinfo.removeClass("error");
            return true;
        }
    }

    function validateNationality(){
        //it's NOT valid
        if(nationality.val().length < 3){
            nationality.addClass("error");
            nationalityinfo.text("Nationality have to be at least 3 letters!");
            nationalityinfo.addClass("error");
            return false;
        }
        //it's valid
        else{
            nationality.removeClass("error");
            nationalityinfo.text("Type your nationality!");
            nationalityinfo.removeClass("error");
            return true;
        }
    }
});